﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Knlght.Domain;
using Knlght.Model.links;
using Knlght.Service;
using Knlght.Web.Infrastructure;
using Knlght.Web.Infrastructure.Menus;
using Knlght.Web.Infrastructure.Mvc;
using Knlght.Web.Areas.Manage.Controllers.Filters;

namespace Knlght.Web.Areas.Manage.Controllers
{
    
        /// <summary>
        /// 友情链接
        /// </summary>
        public class LinkController : BaseAdminController
        {
            [MallAdminMenu("Link_Info")]
            [Permission("Link_Info")]
        public ActionResult Index()
            {
                return View();
            }

            #region AngularJs      
            [HttpPost]
            public JsonResult Paging(PagingParameter parameter)
            {
                var query = new QueryRequest<LinkInfo>();
                if (parameter.Query.CreateCriterionByString(nameof(LinkInfo.Name)) != null)
                {
                    var username = parameter.Query.CreateCriterionByString(nameof(LinkInfo.Name)).Value.ToString();
                    query.Add(o => o.Name.Contains(username));
                }
                if (parameter.Query.CreateCriterionByBoolean(nameof(LinkInfo.IsEnabled)) != null)
                {
                    var isEnabled = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(LinkInfo.IsEnabled)).Value);
                    query.Add(o => o.IsEnabled == isEnabled);
                }
                if (parameter.Query.CreateCriterionByBoolean(nameof(LinkInfo.IsBlank)) != null)
                {
                    var isBlank = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(LinkInfo.IsBlank)).Value);
                    query.Add(o => o.IsBlank == isBlank);
                }
                var service = new LinkInfoService();
                query.AddOrder(nameof(LinkInfo.Name), true);
                query.AddOrder(nameof(LinkInfo.AddTime), false);

                var pagingRequest = new PagingRequest<LinkInfo>
                {
                    PageIndex = parameter.PageIndex,
                    PageSize = parameter.PageSize,
                    Query = query
                };
                var rsp = service.Paging(pagingRequest);
                return EJPagingJson(rsp);
            }
            [HttpPost]
            public JsonResult Add(LinkInfo model, string tempImageUrl)
            {
                model.ImageUrl = UploadHelper.MoveFile(tempImageUrl);
                var service = new LinkInfoService();
                var rsp = service.Add(model);
                return rsp.IsSuccess ? EJActionJson(true, model.Id.ToString()) : EJActionJson(rsp);
            }
            [HttpPost]
            public JsonResult Update(LinkInfo model, string tempImageUrl)
            {
                model.ImageUrl = UploadHelper.MoveFile(tempImageUrl);
                var service = new LinkInfoService();
                var rsp = service.Update(model);
                return EJActionJson(rsp);
            }
            [HttpPost]
            public JsonResult Updates(int[] ids, string property, object value)
            {

                if (property.ToLower() == nameof(LinkInfo.IsEnabled).ToLower())
                {
                    var service = new LinkInfoService();
                    var query = new QueryRequest<LinkInfo>();
                    query.Add(o => ids.Contains(o.Id));
                    var rsp = service.Update(query, i => new LinkInfo { IsEnabled = (bool)value });
                    return EJActionJson(rsp);
                }
                else if (property.ToLower() == nameof(LinkInfo.IsBlank).ToLower())
                {
                    var service = new LinkInfoService();
                    var query = new QueryRequest<LinkInfo>();
                    query.Add(o => ids.Contains(o.Id));
                    var rsp = service.Update(query, i => new LinkInfo { IsBlank = (bool)value });
                    return EJActionJson(rsp);
                }
                else
                {
                    return EJActionJson(false, "该属性修改方法没有实现！");
                }
            }
            [HttpPost]
            public JsonResult Remove(int id)
            {
                var service = new LinkInfoService();
                var query = new QueryRequest<LinkInfo>();
                query.Add(o => o.Id == id);
                var rsp = service.Remove(query);
                return EJActionJson(rsp);
            }
            [HttpPost]
            public JsonResult Removes(int[] ids)
            {
                var service = new LinkInfoService();
                var query = new QueryRequest<LinkInfo>();
                query.Add(o => ids.Contains(o.Id));
                var rsp = service.Remove(query);
                return EJActionJson(rsp);
            }
            const string AdUploadDirectory = "link";
            /// <summary>
            /// 上传文件
            /// </summary>
            [HttpPost]
            public JsonResult Upload()
            {
                var rsp = this.UploadPics(AdUploadDirectory);
                return EJItemJson(rsp);
            }
            #endregion
        }
    }
